<template>
  <div class="register-container">
    <div class="register-card">
      <h3 class="title">计考通系统注册页面</h3>
      <form class="register-form" @submit.prevent="handleRegister">
        <!-- 调整顺序：用户名 -->
        <div class="form-group">
          <label class="label-text" for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="registerForm.username"
            placeholder="请输入用户名"
            class="input-field"
          />
        </div>
        <!-- 调整顺序：密码 -->
        <div class="form-group">
          <label class="label-text" for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="registerForm.password"
            placeholder="请输入密码"
            class="input-field"
          />
        </div>
        <!-- 调整顺序：邮箱 -->
        <div class="form-group">
          <label class="label-text" for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            class="input-field"
          />
        </div>
        <div class="agree-group">
          <input
            type="checkbox"
            id="agree"
            v-model="agreeTerms"
            class="agree-checkbox"
          />
          <label class="agree-text" for="agree">我接受所有条款和条件</label>
        </div>
        <!-- 调整注册按钮颜色为渐变色 -->
        <button type="submit" class="register-btn" :disabled="!agreeTerms">
          注册
        </button>
        <div class="login-link" @click="handleLogin">登录</div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        email: ''
      },
      agreeTerms: false
    }
  },
  methods: {
    async handleRegister() {
      try {
        const response = await this.$axios.post('http://localhost:8080/user/user_add', {
          username: this.registerForm.username,
          password: this.registerForm.password,
          email: this.registerForm.email,
          phone: '',
          realName: '',
          gender: 0,
          birthDate: null,
          lastLogin: null,
          status: 0,
          avatar: '',
          isAdmin: 0
        });
        if (response.data > 0) {
          this.$message.success('注册成功');
          this.$router.push('/login'); 
        } else {
          this.$message.error('注册失败，请稍后重试');
        }
      } catch (error) {
        this.$message.error('注册失败，请检查网络或稍后重试');
      }
    },
    handleLogin() {
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>
/* 大容器样式 */
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(120deg, #E0F7FA 0%, #B2EBF2 100%); 
}

/* 注册卡片样式 */
.register-card {
  width: 340px;
  padding: 40px;
  border-radius: 24px;
  background: linear-gradient(135deg, #4FC3F7 0%, #81D4FA 100%); 
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); 
  color: #fff;
  text-align: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
}

.register-card::before {
  content: "💖";
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 30px;
}

.register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15); 
}

/* 标题样式 */
.title {
  margin-bottom: 32px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase; 
  animation: titleSlide 1s ease-in-out;
}

@keyframes titleSlide {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* 表单组通用样式 */
.form-group {
  margin-bottom: 24px;
  text-align: left;
  animation: formFade 1s ease-in-out;
}

@keyframes formFade {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 标签文本样式 */
.label-text {
  display: block;
  margin-bottom: 8px;
  font-size: 15px;
  color: #fff; 
  font-weight: 500;
}

/* 输入框样式 */
.input-field {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  border: none;
  border-radius: 16px;
  background-color: #E0F7FA; 
  color: #333;
  font-size: 15px;
  transition: box-shadow 0.3s ease, transform 0.3s ease;
}

.input-field:focus {
  outline: none;
  box-shadow: 0 0 12px rgba(79, 195, 247, 0.6); 
  transform: scale(1.02); 
}

/* 协议勾选区域样式 */
.agree-group {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 24px;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.2s;
  opacity: 0;
  animation-fill-mode: forwards;
}

.agree-checkbox {
  width: 18px;
  height: 18px;
  margin-right: 10px;
  accent-color: #03A9F4; 
}

.agree-text {
  font-size: 14px;
  color: #fff; 
}

/* 注册按钮样式（改为渐变色） */
.register-btn {
  width: 100%;
  height: 48px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(90deg, #03A9F4 0%, #29B6F6 100%); 
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.4s;
  opacity: 0;
  animation-fill-mode: forwards;
  position: relative;
}

.register-btn::after {
  content: "🚀";
  position: absolute;
  right: 15px;
  opacity: 0;
  transition: all 0.3s ease;
}

.register-btn:hover {
  box-shadow: 0 4px 16px rgba(3, 169, 244, 0.4); 
  transform: translateY(-2px);
}

.register-btn:hover::after {
  opacity: 1;
  right: 20px;
}

.register-btn:active {
  transform: scale(0.96);
}

.register-btn:disabled {
  background: #bdc3c7;
  color: #fff;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* 登录链接样式 */
.login-link {
  margin-top: 20px;
  font-size: 15px;
  color: #fff; 
  text-decoration: underline;
  cursor: pointer;
  transition: color 0.3s ease, transform 0.3s ease;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.6s;
  opacity: 0;
  animation-fill-mode: forwards;
}

.login-link:hover {
  color: #03A9F4; 
  transform: translateY(-1px);
}

@keyframes formFade {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>